
const mybehavior = require("../../../../behaviors/requests")
import WxValidate from "../../../../Wx_Validate/WxValidate"
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    base_urlstr: "https://logisticsengineering.gree.com/package_integrated_api/reverse_manage",
    popup_date_show: false,
    minDate: new Date().getTime(),
    maxDate: new Date(2119, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    name: "",
    docking_person: "",
    docking_person_phone_num: "",
    require_time: "",
    requite_tonnage: "",
    address: "",
    material_type: "",
    material_source: "",
    popup_show0: false,
    options0: ["3吨", "5吨", "16吨"],
    openid: ""
  },
  // 时分的事件方法
  selectDateMinuteChange(e) {
    let now_time = this.formatNowTime()
    console.log(now_time);
    console.log(e.detail.value);
    console.log(e.detail.value < now_time);
    if (e.detail.value < now_time) {
      return Toast('需求时间要求大于当前时间')
    } else {
      this.setData({
        require_time: e.detail.value
      })
    }

  },
  // 验证手机号
  onInputPhone(e) {
    let T = this.checkPhone(e.detail)
    if (!T) {
      wx.showToast({
        title: '手机格式错误',
        icon: 'error'
      })
    }
  },
  // 点击需求吨位输入框
  onClick0(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show0: true
    })
  },
  // 选择需求吨位输入框关闭
  onClose0() {
    this.setData({ popup_show0: false });
  },
  // 确定责需求吨位--弹出框关闭
  onChange0(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      requite_tonnage: value,
      popup_show0: false,
    })
  },

  // onInput(event) {
  //   // let now_time = (new Date()).valueOf()
  //   this.setData({
  //     currentDate: event.detail,
  //   });
  // },

  // 确定开始日期输入框
  onDateConfirm(e) {
    this.setData({
      popup_date_show: false,
      require_time: this.formatDate(new Date(e.detail)),
    });
  },
  // 开始日期输入框关闭
  onDateClose() {
    this.setData({
      popup_date_show: false,
      currentDate: null
    });
  },
  // 开始点击输入框显示选择日期
  onDateClick() {
    this.setData({
      popup_date_show: true,

    });
  },

  // 点击提交按钮
  formSubmit(e) {
    const cur_t = this.formatNowTime()
    if (this.data.require_time < cur_t) {
      this.setData({
        require_time: ""
      })
      return Toast('需求时间要求大于当前时间')
    } else {
      let that = this
      let url = that.data.base_urlstr + "/release_requirements";
      let data = {
        docking_person: this.data.docking_person,
        docking_person_phone_num: this.data.docking_person_phone_num,
        require_time: this.data.require_time,
        requite_tonnage: this.data.requite_tonnage,
        address: this.data.address,
        material_type: this.data.material_type,
        material_source: this.data.material_source,
      }
      console.log("123123", data);
      if (!that.WxValidate.checkForm(data)) {
        const error = that.WxValidate.errorList[0]
        Toast(error.msg)
      } else {
        that.post(url, data).then(res => {
          console.log("返回结果", res)
          if (res.code == 20000) {
            wx.showToast({
              title: res.msg,
              icon: "success"
            });
            this.formReset()
          } else {
            wx.showToast({
              title: res.msg,
              icon: "error"
            })
          }
        }).catch(err => {
          wx.showToast({
            title: err.msg,
            icon: 'error'
          })
        })
      }
    }


    // console.log(data)
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      docking_person: {
        required: true,
      },
      docking_person_phone_num: {
        required: true,
      },
      require_time: {
        required: true,
      },
      requite_tonnage: {
        required: true,
      },
      address: {
        required: true,
      },
      material_type: {
        required: true,
      },
      material_source: {
        required: true,
      },

    }

    let message = {
      docking_person: {
        required: "对接人不能为空",
      },
      docking_person_phone_num: {
        required: "电话号码不能为空",
      },
      require_time: {
        required: "需求时间不能为空",
      },
      requite_tonnage: {
        required: "需求吨位不能为空",
      },
      address: {
        required: "用车地点不能为空",
      },
      material_type: {
        required: "物料类型不能为空",
      },
      material_source: {
        required: "物料来源不能为空",
      },




    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 点击重置按钮
  formReset() {
    this.setData({
      docking_person: "",
      docking_person_phone_num: "",
      require_time: "",
      requite_tonnage: "",
      address: "",
      material_type: "",
      material_source: "",
    })
  },

  sentMsg() {
    // o4Fpp5f3gT3lirMgnZJjLiQbb-vM 亚辉
    // o4Fpp5cwgzGPIoC4jfVtBJhX17rs
    let openid = "o4Fpp5f3gT3lirMgnZJjLiQbb-vM"
    // let openid = wx.getStorageSync('openid');

    let data = {
      time2: "2023年7月31日",
      thing4: "测试4",
      date6: "2023年7月31日",
      name1: "测试",
      phone_number11: "16675608673",
      openid: openid,
      template_id: 'xZBjmnXt_v3KdMl8WtX_gjeKng3oNvxmnnY8RI0O59o',
    }
    console.log("11", data);
    let url = 'http://127.0.0.1:5000/send_msg'
    wx.request({
      url,
      data,
      dataType: "json",
      method: "POST",
      success(res) {
        console.log(res.data);
        if (res.data.code == 20000) {
          wx.showToast({
            title: '发送成功',
            icon: 'success'
          })
        } else {
        }
      },
      fail(res) {
        console.log(res);
      }
    });
  },

 




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      minDate: new Date().getTime(),
      currentDate: new Date().getTime(),
      name: wx.getStorageSync('name')
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})